# Loughney et al., "Tectonic influence on Cenozoic mammal richness and sedimentation history of the Basin and Range, western North America"
# Science Advances
# R code to plot Figure 6
# final published figure processed in Adobe Illustrator CC

# load data file
macroLith <- read.csv("macrostrat_lithology.csv", header = TRUE, stringsAsFactors = FALSE)

# load required packages
library(ggplot2)
library(ggfortify)
library(dplyr)

# ---------------------------------------------------------------------------------------------------
# Figure 6 - sedimentary Macrostrat package lithology through time
# re-order lithologies on thickness
lithOrder <- factor(macroLith$Lithology, levels = c("Coal", "Evaporites", "Limestone and Diatomite", "Mud and Shale", "Sand", "Conglomerate and Gravel", "Breccia", "Siliciclastics", "Volcaniclastics"))

# calculate proportion with dplyr
lithProp <- macroLith  %>%
  group_by(Age, Lithology) %>%
  summarise(n = sum(Thickness)) %>%
  mutate(proportion = n / sum(n))

# re-order lithologies on proportion
lithOrder <- factor(lithProp$Lithology, levels = c("Coal", "Evaporites", "Limestone and Diatomite", "Mud and Shale", "Sand", "Conglomerate and Gravel", "Breccia", "Siliciclastics", "Volcaniclastics"))

# plot
ggplot(lithProp, aes(x = Age, y = proportion, fill = lithOrder)) + 
    geom_area(alpha = 0.75 , size = 0.5, colour = "black") +
	scale_fill_brewer(palette = "Paired") +
	scale_x_reverse(breaks = seq(0, 36, 2))

